﻿using System;
using System.Data;

using NxERP.Bll;

namespace LengthAiCalculate
{
    public class Data
    {
        public static SqlHelper sqlHelper = new SqlHelper(SysInit.DecryptDbConn());

        public static DataTable GetJhDate()
        {
            return sqlHelper.GetDataTableFromSql($"SELECT id,lg,num FROM [PM.AiJh] WHERE (isdel=0 or isdel is null and userCode='{LoginUser.LoginUserCode}') ORDER BY lg desc");
        }

        public static DataTable GetScDate()
        {
            return sqlHelper.GetDataTableFromSql($"SELECT id,lg,num FROM [PM.AiSc]  WHERE (isdel=0 or isdel is null and userCode='{LoginUser.LoginUserCode}') ORDER BY num desc");
        }

        public static DataTable ChangeDataTableDataType(DataTable dt)
        {
            DataTable dtResult = new DataTable();
            dtResult = dt.Clone();
            foreach (DataColumn col in dtResult.Columns)
            {
                if (col.ColumnName.ToLower() == "lg")
                {
                    //修改列类型
                    col.DataType = typeof(decimal);
                }
                else if (col.ColumnName.ToLower() == "num")
                {
                    col.DataType = typeof(Int32);
                }
            }
            foreach (DataRow row in dt.Rows)
            {
                DataRow newDtRow = dtResult.NewRow();
                foreach (DataColumn column in dt.Columns)
                {
                    if (column.ColumnName.ToLower() == "lg")
                    {
                        newDtRow[column.ColumnName] = Convert.ToDecimal(row[column.ColumnName]);
                    }
                    else if (column.ColumnName.ToLower() == "num")
                    {
                        newDtRow[column.ColumnName] = Convert.ToInt32(row[column.ColumnName]);
                    }
                }
                dtResult.Rows.Add(newDtRow);
            }
            return dtResult;
        }
    }
}